﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillReshipmentGoodsService
    {
        /// <summary>
        /// 查询退货单商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillReshipmentGoods GetBillReshipmentGoods(long id)
        {
            BillReshipmentGoods reshipmentGoods = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    reshipmentGoods = new BillReshipmentGoods();
                    reshipmentGoods.Id = id;
                    reshipmentGoods = new BillReshipmentGoodsDAO().GetBillReshipmentGoods(conn, reshipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return reshipmentGoods;
        }

        /// <summary>
        /// 查询退货单商品列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面数据量</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="reshipmentno">退货单号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="memberno">客户码</param>
        /// <param name="adminUsername">退货人</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="ascfield">升序字段</param>
        /// <param name="descfield">降序字段</param>
        /// <returns></returns>
        public DataTable GetBillReshipmentGoodsTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String reshipmentno, String shipmentno, 
            String barcode, String memberno, String adminUsername, String startdate, String enddate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillReshipmentGoods reshipmentGoods = new BillReshipmentGoods();
                    reshipmentGoods.Pageindex = pageindex;
                    reshipmentGoods.Pagesize = pagesize;
                    reshipmentGoods.EnterpriseNo = enterpriseNo;
                    reshipmentGoods.StoreNo = storeNo;
                    reshipmentGoods.Reshipmentno = reshipmentno;
                    reshipmentGoods.Shipmentno = shipmentno;
                    reshipmentGoods.Barcode = barcode;
                    reshipmentGoods.Memberno = memberno;
                    reshipmentGoods.AdminUsername = adminUsername;
                    reshipmentGoods.StartDate = startdate;
                    reshipmentGoods.EndDate = enddate;
                    reshipmentGoods.Ascfield = ascfield;
                    reshipmentGoods.Descfield = descfield;
                    dt = new BillReshipmentGoodsDAO().GetBillReshipmentGoodsTable(conn, reshipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询退货单商品记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="reshipmentno">退货单号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="memberno">客户码</param>
        /// <param name="adminUsername">退货人</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <returns></returns>
        public long GetBillReshipmentGoodsRecord(String enterpriseNo, String storeNo, String reshipmentno, String shipmentno, String barcode,
            String memberno, String adminUsername, String startdate, String enddate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillReshipmentGoods reshipmentGoods = new BillReshipmentGoods();
                    reshipmentGoods.EnterpriseNo = enterpriseNo;
                    reshipmentGoods.StoreNo = storeNo;
                    reshipmentGoods.Reshipmentno = reshipmentno;
                    reshipmentGoods.Shipmentno = shipmentno;
                    reshipmentGoods.Barcode = barcode;
                    reshipmentGoods.Memberno = memberno;
                    reshipmentGoods.AdminUsername = adminUsername;
                    reshipmentGoods.StartDate = startdate;
                    reshipmentGoods.EndDate = enddate;
                    record = new BillReshipmentGoodsDAO().GetBillRehipmentGoodsRecord(conn, reshipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入退货单商品
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertBillReshipmentGoods(DbConnection conn, ArrayList list)
        {
            int count = 0;
            try
            {
                if (list != null && list.Count > 0)
                {
                    BillReshipmentGoodsDAO reshipmentGoodsDAO = new BillReshipmentGoodsDAO();
                    foreach (BillReshipmentGoods lopGoods in list)
                    {
                        long id = reshipmentGoodsDAO.InsertBillReshipmentGoods(conn, lopGoods);
                        if (id > 0)
                        {
                            count += 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 删除退货单商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBillReshipmentGoods(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        BillReshipmentGoodsDAO reshipmentGoodsDAO = new BillReshipmentGoodsDAO();
                        BillReshipmentGoods reshipmentGoods = new BillReshipmentGoods();
                        foreach (String lopid in arr)
                        {
                            reshipmentGoods.Id = Int64.Parse(lopid);
                            count += reshipmentGoodsDAO.DeleteBillReshipmentGoods(conn, reshipmentGoods);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 删除退货单商品
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="reshipmentno"></param>
        /// <returns></returns>
        public int DeleteBillReshipmentGoods(DbConnection conn, String reshipmentno)
        {
            int count = 0;
            try
            {
                BillReshipmentGoodsDAO reshipmentGoodsDAO = new BillReshipmentGoodsDAO();
                BillReshipmentGoods reshipmentGoods = new BillReshipmentGoods();
                reshipmentGoods.Reshipmentno = reshipmentno;
                count += reshipmentGoodsDAO.DeleteBillReshipmentGoods(conn, reshipmentGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 退货商品是否已经存在
        /// </summary>
        /// <param name="shipmentno"></param>
        /// <param name="barcode"></param>
        /// <param name="goodsNorm"></param>
        /// <returns></returns>
        public long ExistBillReshipmentGoods(String shipmentno, String barcode, String goodsNorm)
        {
            long count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillReshipmentGoods reshipmentGoods = new BillReshipmentGoods();
                    reshipmentGoods.Shipmentno = shipmentno;
                    reshipmentGoods.Barcode = barcode;
                    reshipmentGoods.GoodsNorm = goodsNorm;
                    count = new BillReshipmentGoodsDAO().ExistBillReshipmentGoods(conn, reshipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
